#!/usr/bin/env bash

set -Eeo pipefail

function init() {
	if [ -f /data/fspbx/config.sh ]
	then
		cp -rp /data/fspbx/etc/fusionpbx/* /etc/fusionpbx/
	else
		# copy freeswitch
		mkdir -p /data/fspbx/etc
		mkdir -p /data/fspbx/etc/fusionpbx
		mkdir -p /data/fspbx/var/lib
		mkdir -p /data/fspbx/var/log
		mkdir -p /data/fspbx/usr/share
		mkdir -p /data/fspbx/var/cache
		mkdir -p /data/fspbx/tmp
		cp -rp /etc/freeswitch 			/data/fspbx/etc/
		cp -rp /var/lib/freeswitch 		/data/fspbx/var/lib/
		cp -rp /var/log/freeswitch 		/data/fspbx/var/log/
		cp -rp /usr/share/freeswitch 	/data/fspbx/usr/share/
		cp -rp /var/cache/freeswitch 	/data/fspbx/var/cache/
		mkdir -p /data/fspbx/usr/share/freeswitch/sounds/music/default
		chown -R freeswitch:daemon      /data/fspbx
		cp -rp /data/fspbx/usr/share/freeswitch/sounds/music/*000 /data/fspbx/usr/share/freeswitch/sounds/music/default/

		#update the permissions
		chown -R freeswitch:daemon  /var/www/fusionpbx
		find /data/fspbx/etc/freeswitch -type d -exec chmod 770 {} \;
		find /data/fspbx/var/lib/freeswitch -type d -exec chmod 770 {} \;
		find /data/fspbx/var/log/freeswitch -type d -exec chmod 770 {} \;
		find /usr/share/freeswitch -type d -exec chmod 770 {} \;
		find /data/fspbx/etc/freeswitch -type f -exec chmod 664 {} \;
		find /data/fspbx/var/lib/freeswitch -type f -exec chmod 664 {} \;
		find /data/fspbx/var/log/freeswitch -type f -exec chmod 664 {} \;
		find /data/fspbx/usr/share/freeswitch -type f -exec chmod 664 {} \;

		#update xml_cdr url, user and password
		xml_cdr_username=$(dd if=/dev/urandom bs=1 count=12 2>/dev/null | base64 | sed 's/[=\+//]//g')
		xml_cdr_password=$(dd if=/dev/urandom bs=1 count=12 2>/dev/null | base64 | sed 's/[=\+//]//g')
		sed -i /data/fspbx/etc/freeswitch/autoload_configs/xml_cdr.conf.xml -e s:"{v_http_protocol}:http:"
		sed -i /data/fspbx/etc/freeswitch/autoload_configs/xml_cdr.conf.xml -e s:"{domain_name}:127.0.0.1:"
		sed -i /data/fspbx/etc/freeswitch/autoload_configs/xml_cdr.conf.xml -e s:"{v_project_path}::"
		sed -i /data/fspbx/etc/freeswitch/autoload_configs/xml_cdr.conf.xml -e s:"{v_user}:$xml_cdr_username:"
		sed -i /data/fspbx/etc/freeswitch/autoload_configs/xml_cdr.conf.xml -e s:"{v_pass}:$xml_cdr_password:"

		# start freeswitch
		/usr/bin/freeswitch -ncwait -nonat 	\
			-base /usr \
			-conf /data/fspbx/etc/freeswitch \
			-log /data/fspbx/var/log/freeswitch \
			-run /var/run/freeswitch \
			-db /data/fspbx/var/lib/freeswitch/db \
			-mod /usr/lib64/freeswitch/mod \
			-htdocs /data/fspbx/usr/share/freeswitch/htdocs \
			-scripts /data/fspbx/usr/share/freeswitch/scripts \
			-temp /data/fspbx/tmp \
			-grammar /data/fspbx/usr/share/freeswitch/grammar \
			-certs /data/fspbx/etc/freeswitch/tls \
			-recordings /data/fspbx/var/lib/freeswitch/recordings \
			-storage /data/fspbx/var/lib/freeswitch/storage \
			-cache /data/fspbx/var/cache/freeswitch \
			-sounds /data/fspbx/usr/share/freeswitch/sounds

		#update the permissions
		find /var/www/fusionpbx -type d -exec chmod 770 {} \;
		find /var/www/fusionpbx -type f -exec chmod 664 {} \;

		#database details
		database_host=127.0.0.1
		database_port=5432
		database_username=fusionpbx
		fusionpbx_password=$(dd if=/dev/urandom bs=1 count=20 2>/dev/null | base64 | sed 's/[=\+//]//g')
		freeswitch_password=$(dd if=/dev/urandom bs=1 count=20 2>/dev/null | base64 | sed 's/[=\+//]//g')

		#create database and users
		psql --host=$database_host --port=$database_port --username=postgres -c "CREATE DATABASE fusionpbx";
		psql --host=$database_host --port=$database_port --username=postgres -c "CREATE DATABASE freeswitch";
		psql --host=$database_host --port=$database_port --username=postgres -c "CREATE ROLE fusionpbx WITH SUPERUSER LOGIN PASSWORD '$fusionpbx_password';"
		psql --host=$database_host --port=$database_port --username=postgres -c "CREATE ROLE freeswitch WITH SUPERUSER LOGIN PASSWORD '$freeswitch_password';"
		psql --host=$database_host --port=$database_port --username=postgres -c "GRANT ALL PRIVILEGES ON DATABASE fusionpbx to fusionpbx;"
		psql --host=$database_host --port=$database_port --username=postgres -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to fusionpbx;"
		psql --host=$database_host --port=$database_port --username=postgres -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to freeswitch;"

		sed -i /etc/fusionpbx/config.php -e s:"{database_host}:$database_host:"
		sed -i /etc/fusionpbx/config.php -e s:'{database_username}:fusionpbx:'
		sed -i /etc/fusionpbx/config.php -e s:"{database_password}:$fusionpbx_password:"

		#add the database schema
		cd /var/www/fusionpbx && php /var/www/fusionpbx/core/upgrade/upgrade_schema.php > /dev/null 2>&1

		#get the server hostname
		#domain_name=$(hostname -f)
		domain_name=$(hostname -I | cut -d ' ' -f1)
		domain_uuid=$(php /var/www/fusionpbx/resources/uuid.php);
		psql --host=$database_host --port=$database_port --username=$database_username -c "insert into v_domains (domain_uuid, domain_name, domain_enabled) values('$domain_uuid', '$domain_name', 'true');"
		#app defaults
		cd /var/www/fusionpbx && php /var/www/fusionpbx/core/upgrade/upgrade_domains.php

		#add the user
		user_uuid=$(/usr/bin/php /var/www/fusionpbx/resources/uuid.php);
		user_salt=$(/usr/bin/php /var/www/fusionpbx/resources/uuid.php);
		system_username=admin
		system_password=$(dd if=/dev/urandom bs=1 count=12 2>/dev/null | base64 | sed 's/[=\+//]//g')
		user_name=$system_username
		user_password=$(dd if=/dev/urandom bs=1 count=12 2>/dev/null | base64 | sed 's/[=\+//]//g')
		password_hash=$(php -r "echo md5('$user_salt$user_password');");
		psql --host=$database_host --port=$database_port --username=$database_username -t -c "insert into v_users (user_uuid, domain_uuid, username, password, salt, user_enabled) values('$user_uuid', '$domain_uuid', '$user_name', '$password_hash', '$user_salt', 'true');"

		#get the superadmin group_uuid
		group_uuid=$(psql --host=$database_host --port=$database_port --username=$database_username -t -c "select group_uuid from v_groups where group_name = 'superadmin';");
		group_uuid=$(echo $group_uuid | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//')
		user_group_uuid=$(/usr/bin/php /var/www/fusionpbx/resources/uuid.php);
		group_name=superadmin
		psql --host=$database_host --port=$database_port --username=$database_username -c "insert into v_user_groups (user_group_uuid, domain_uuid, group_name, group_uuid, user_uuid) values('$user_group_uuid', '$domain_uuid', '$group_name', '$group_uuid', '$user_uuid');"

		#app defaults
		cd /var/www/fusionpbx && php /var/www/fusionpbx/core/upgrade/upgrade_domains.php

		# stop freeswitch
		/usr/bin/freeswitch -stop;

		sleep 10;

		cat << EOF > /data/fspbx/config.sh
database_host=$database_host
database_port=$database_port
database_username=$database_username
fusionpbx_password=$fusionpbx_password
freeswitch_password=$freeswitch_password
domain_name=$domain_name
domain_uuid=$domain_uuid
user_uuid=$user_uuid
user_salt=$user_salt
system_username=$system_username
system_password=$system_password
user_name=$user_name
user_password=$user_password
password_hash=$password_hash
group_uuid=$group_uuid
user_group_uuid=$user_group_uuid
group_name=$group_name
xml_cdr_username=$xml_cdr_username
xml_cdr_password=$xml_cdr_password
EOF

		cp    /etc/fusionpbx/*   /data/fspbx/etc/fusionpbx/
		chown -R freeswitch:daemon      /data/fspbx
	fi
}

if [ "${1:0:1}" = '-' ]; then
    set -- supervisord "$@"
fi

init

exec "$@"